﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Globalization;
using System.Web.UI.HtmlControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisEducations : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IHisEducationBLL eduBLL = new HisEducationBLL();
        private IMasEducationDegreeBLL degreeBLL = new MasEducationDegreeBLL();
        private IMasEducationQualificationBLL qualBLL = new MasEducationQualificationBLL();
        private IMasEducationMajorBLL majBLL = new MasEducationMajorBLL();
		private IMasEducationInstituteBLL insBLL = new MasEducationInstituteBLL();
		private Main main = new Main();
        //private IMasCountryBLL countryBLL = new MasCountryBLL();
        private const int ItemsPerRequest = 10;
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblEmpSeq.Text = Session["emp_seq"].ToString();

            if (DataItem != null)
            {
                bindYearCombo();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    LoadDegree();

                    lblEduSeq.Text = DataBinder.Eval(DataItem, "EDU_SEQ").ToString();

                    ddlEduDegreeSeq.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "EDU_DEGREE_SEQ"));
                    lblDegreeSeq.Text = Convert.ToString(DataBinder.Eval(DataItem, "EDU_DEGREE_SEQ"));
                    cmbQual.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_QUAL_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_QUAL_SEQ").ToString()));
                    cmbMajor.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_MAJOR_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_MAJOR_SEQ").ToString()));
                    cmbInstitutes.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_INS_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_INS_SEQ").ToString()));
                    
                    cmbQual.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "EDU_QUAL_SEQ"));
                    cmbMajor.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "EDU_MAJOR_SEQ"));
                    cmbInstitutes.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "EDU_INS_SEQ"));

                    if (cmbQual.SelectedIndex > 0)
                    {
                        if (DataBinder.Eval(DataItem, "COUNTRY_SEQ").ToString() == "140")
                            rdoInsLocal.SelectedValue = "1";
                        else
                            rdoInsLocal.SelectedValue = "0";
                    }

                    txtEduStartDate.SetValue(DataBinder.Eval(DataItem, "START_DATE", "{0:dd/MM/yyyy}"));
                    txtEduEndDate.SetValue(DataBinder.Eval(DataItem, "END_DATE", "{0:dd/MM/yyyy}"));
                    if (DataBinder.Eval(DataItem, "EDU_START_YEAR") != null)
                        ddlEduStartYear.SelectedValue = DataBinder.Eval(DataItem, "EDU_START_YEAR").ToString();
                    if (DataBinder.Eval(DataItem, "EDU_END_YEAR") != null)
                        ddlEduEndYear.SelectedValue = DataBinder.Eval(DataItem, "EDU_END_YEAR").ToString();
                    txtGPA.Text = Convert.ToString(DataBinder.Eval(DataItem, "GPA", "{0:N2}"));
                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();

                }
                else
                {

                }
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void LoadDegree()
        {
            IList<MasEducationDegreeInfo> infoList = degreeBLL.ListEducationDegree();
            ddlEduDegreeSeq.DataSource = infoList;
            ddlEduDegreeSeq.DataBind();
        }
        public void bindYearCombo()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 50;
            int MaxYear = curYear + 50;
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            ddlEduStartYear.DataSource = dt;
            ddlEduStartYear.DataBind();
            ddlEduStartYear.SelectedValue = curYear.ToString();

            ddlEduEndYear.DataSource = dt;
            ddlEduEndYear.DataBind();
            ddlEduEndYear.SelectedValue = curYear.ToString();

        }
        
 
        //============================= ControlEvents ======================================

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            eduBLL.Insert(lblEmpSeq.Text
                , ddlEduDegreeSeq.SelectedValue
                , cmbQual.SelectedValue
                , cmbMajor.SelectedValue
                , cmbInstitutes.SelectedValue
                , ddlEduStartYear.SelectedValue.Length > 0 ? (Nullable<int>) Int32.Parse(ddlEduStartYear.SelectedValue) : null
                , ddlEduEndYear.SelectedValue.Length > 0 ? (Nullable<int>) Int32.Parse(ddlEduEndYear.SelectedValue) : null
                , (txtEduStartDate.GetStringValue().Length > 0 ? (Nullable<DateTime>)DateTime.Parse(txtEduStartDate.GetStringValue()) : null)
                , (txtEduEndDate.GetStringValue().Length > 0 ? (Nullable<DateTime>)DateTime.Parse(txtEduEndDate.GetStringValue()) : null)
                , txtGPA.Text.Length > 0 ? (Nullable<decimal>) decimal.Parse(txtGPA.Text) : null
                , txtRemarks.Text.Trim()
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            eduBLL.Update(Int32.Parse(lblEduSeq.Text.Trim())
                , lblEmpSeq.Text
                , ddlEduDegreeSeq.SelectedValue
                , cmbQual.SelectedValue
                , cmbMajor.SelectedValue
                , cmbInstitutes.SelectedValue
                , ddlEduStartYear.SelectedValue.Length > 0 ? (Nullable<int>)Int32.Parse(ddlEduStartYear.SelectedValue) : null
                , ddlEduEndYear.SelectedValue.Length > 0 ? (Nullable<int>)Int32.Parse(ddlEduEndYear.SelectedValue) : null
                , (txtEduStartDate.GetStringValue().Length > 0 ? (Nullable<DateTime>)DateTime.Parse(txtEduStartDate.GetStringValue()) : null)
                , (txtEduEndDate.GetStringValue().Length > 0 ? (Nullable<DateTime>)DateTime.Parse(txtEduEndDate.GetStringValue()) : null)
                , txtGPA.Text.Length > 0 ? (Nullable<decimal>)decimal.Parse(txtGPA.Text) : null
                , txtRemarks.Text.Trim()
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void ddlEduDegreeSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadDegree();
        }

        protected void cmbQual_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbQual.Items.Clear();
            Nullable<int> DegreeSeq = string.IsNullOrEmpty(lblDegreeSeq.Text) ? (int?)null : Int32.Parse(lblDegreeSeq.Text);

            DataSet ds = null;
            DataTable dt = null;

            ds = qualBLL.LoadEducationQualifications((int?)null, DegreeSeq, e.Text);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbQual.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_QUAL_NAME"].ToString(), dt.Rows[i]["EDU_QUAL_SEQ"].ToString()));
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbMajor_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbMajor.Items.Clear();

            DataSet ds = null;
            DataTable dt = null;

            ds = majBLL.LoadEducationMajors((int?)null, e.Text);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
           int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbMajor.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_MAJOR_NAME"].ToString(), dt.Rows[i]["EDU_MAJOR_SEQ"].ToString()));
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbInstitutes_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbInstitutes.Items.Clear();
            bool bLocal = rdoInsLocal.SelectedIndex == 0;

            DataSet ds = null;
            DataTable dt = null;

            ds = insBLL.LoadEducationInstitutes((int?)null, bLocal, e.Text);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbInstitutes.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_INS_NAME"].ToString(), dt.Rows[i]["EDU_INS_SEQ"].ToString()));
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void ddlEduDegreeSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            lblDegreeSeq.Text = ddlEduDegreeSeq.SelectedValue;
        }              
        
    }
}